package com.manager.plat.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IAnalysisDao;
import com.manager.plat.dao.IConfigLowerPartnerDao;
import com.manager.plat.dao.IConfigRouterDao;
import com.manager.plat.util.ExcelCreate;
import com.manager.plat.util.PageInfo;
import com.manager.plat.vo.AnalysisVo;
import com.manager.plat.vo.UserBillVo;
import com.manager.plat.vo.ConfigLowerPartnerVo;
import com.manager.plat.vo.TermVo;
import com.opensymphony.xwork2.ActionSupport;

public class AnalysisAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1554753494153729059L;

	private static Logger logger = Logger.getLogger(AnalysisAction.class);
	
	private IAnalysisDao analysisDao;
	private IConfigLowerPartnerDao configLowerPartnerDao;//渠道信息数据接口
	private IConfigRouterDao configRouterDao;			//路由信息数据接口
	private List<AnalysisVo> analysisVoList;		//数据分析信息VO
	private List<ConfigLowerPartnerVo> configLowerPartnerVoList;//渠道信息列表
	private List<String> spnumberList;					//长号码信息列表
	private List<UserBillVo> userBillVoList;			//账单信息列表
	private TermVo term;								//查询条件VO
	private String exportName;							//导出表格名称
	
	private PageInfo pageInfo;
	private String pageIndexs;
	
	/**进入省份统计结页面*/
	public String getProvince(){
		/**获得渠道信息列表*/
		configLowerPartnerVoList = configLowerPartnerDao.getConfigLowerPartnerList();
		spnumberList= null;
		term = null;
		analysisVoList = null;
		
		return SUCCESS;
	}
	/**查询省份统计信息*/
	public String getProvinceAnalysis(){
		/**获得业务号码*/
		String sp_number = term.getSp_number();
		/**获得渠道信息*/
		int lower_id = term.getLower_id();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		/**根据渠道ID获得渠道长号码列表*/
		spnumberList = configRouterDao.getSpnumberPartnerList(lower_id);
		
		/**根据查询条件获得数据分析信息用户数量*/
		Map<String, Integer> analysisVoMap = analysisDao.getProvinceAnalysisUserNum(sp_number, lower_id, time_begin, time_end);
		
		analysisVoList = analysisDao.getProvinceAnalysis(sp_number, lower_id, time_begin, time_end);
		if(analysisVoList != null && analysisVoList.size() > 0){
			Map<String, AnalysisVo> analysisVoListMap = new HashMap<String, AnalysisVo>();
			for(AnalysisVo analysisVo : analysisVoList){
				/**获得省份*/
				String province = analysisVo.getProvince();
				/**获得是否过滤*/
				int isfilter = analysisVo.getIsfilter();
				logger.debug("getProvinceAnalysis：省份：" + analysisVo.getProvince() + ", 是否过滤:" + analysisVo.getIsfilter());
				if(analysisVoListMap.get(province) != null){
					
					logger.debug("getProvinceAnalysis：" + "isfilter:" + isfilter + ", user_sum：" + analysisVo.getFee_user_num_sum() + ", minute_sum:" + analysisVo.getFee_minute_sum() +
							", price_sum:" + analysisVo.getFee_price_sum());
					logger.debug("getProvinceAnalysis：user：" + analysisVo.getFee_user_num() + ", minute:" + analysisVo.getFee_minute() +
							", price:" + analysisVo.getFee_price());
					
					analysisVo.setFee_user_num_sum(analysisVoListMap.get(province).getFee_user_num() + analysisVo.getFee_user_num());
					analysisVo.setFee_minute_sum(analysisVoListMap.get(province).getFee_minute_sum() + analysisVo.getFee_minute());
					analysisVo.setFee_price_sum(analysisVoListMap.get(province).getFee_price_sum() + analysisVo.getFee_price());
					
					
					if(isfilter == 0){
						analysisVoListMap.get(province).setFee_user_num(analysisVo.getFee_user_num());
						analysisVoListMap.get(province).setFee_minute(analysisVo.getFee_minute());
						analysisVoListMap.get(province).setFee_price(analysisVo.getFee_price());
					}else{
						analysisVoListMap.get(province).setFee_user_num_filter(analysisVo.getFee_user_num());
						analysisVoListMap.get(province).setFee_minute_filter(analysisVo.getFee_minute());
						analysisVoListMap.get(province).setFee_price_filter(analysisVo.getFee_price());
					}
				}else{
					logger.debug("getProvinceAnalysis_1：" + "isfilter:" + isfilter + ", user_sum：" + analysisVo.getFee_user_num_sum() + ", minute_sum:" + analysisVo.getFee_minute_sum() +
							", price_sum:" + analysisVo.getFee_price_sum());
					logger.debug("getProvinceAnalysis_1：user：" + analysisVo.getFee_user_num() + ", minute:" + analysisVo.getFee_minute() +
							", price:" + analysisVo.getFee_price());
					analysisVo.setFee_user_num_sum(analysisVo.getFee_user_num());
					analysisVo.setFee_minute_sum(analysisVo.getFee_minute());
					analysisVo.setFee_price_sum(analysisVo.getFee_price());
					if(isfilter == 1){
						analysisVo.setFee_user_num(0);
						analysisVo.setFee_user_num_filter(analysisVo.getFee_user_num());
						analysisVo.setFee_minute(0);
						analysisVo.setFee_minute_filter(analysisVo.getFee_minute());
						analysisVo.setFee_price(0);
						analysisVo.setFee_price_filter(analysisVo.getFee_price());
					}
					analysisVoListMap.put(province, analysisVo);
				}
				
				
			}
			
			if(analysisVoListMap != null && analysisVoListMap.size() > 0){
				analysisVoList.clear();
				for(AnalysisVo analysisVo : analysisVoListMap.values()){
					if(analysisVoMap != null && analysisVoMap.size() > 0){
						analysisVo.setUser_num(analysisVoMap.get(analysisVo.getProvince()));
					}
					analysisVoList.add(analysisVo);
				}
			}
		}
		
		return SUCCESS;
	}
	
	public String exportAnalsisProvince(){
		/**获得业务号码*/
		String sp_number = term.getSp_number();
		/**获得渠道信息*/
		int lower_id = term.getLower_id();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		/**根据渠道ID获得渠道长号码列表*/
		spnumberList = configRouterDao.getSpnumberPartnerList(lower_id);
		
		/**根据查询条件获得数据分析信息用户数量*/
		Map<String, Integer> analysisVoMap = analysisDao.getProvinceAnalysisUserNum(sp_number, lower_id, time_begin, time_end);
		
		analysisVoList = analysisDao.getProvinceAnalysis(sp_number, lower_id, time_begin, time_end);
		if(analysisVoList != null && analysisVoList.size() > 0){
			Map<String, AnalysisVo> analysisVoListMap = new HashMap<String, AnalysisVo>();
			for(AnalysisVo analysisVo : analysisVoList){
				/**获得省份*/
				String province = analysisVo.getProvince();
				/**获得是否过滤*/
				int isfilter = analysisVo.getIsfilter();
				if(analysisVoListMap.get(province) != null){
					analysisVo.setFee_user_num_sum(analysisVo.getFee_user_num_sum() + analysisVo.getFee_user_num());
					analysisVo.setFee_minute_sum(analysisVo.getFee_minute_sum() + analysisVo.getFee_minute());
					analysisVo.setFee_price_sum(analysisVo.getFee_price_sum() + analysisVo.getFee_price());
					
					if(isfilter == 0){
						analysisVoListMap.get(province).setFee_user_num(analysisVo.getFee_user_num());
						analysisVoListMap.get(province).setFee_minute(analysisVo.getFee_minute());
						analysisVoListMap.get(province).setFee_price(analysisVo.getFee_price());
					}else{
						analysisVoListMap.get(province).setFee_user_num_filter(analysisVo.getFee_user_num());
						analysisVoListMap.get(province).setFee_minute_filter(analysisVo.getFee_minute());
						analysisVoListMap.get(province).setFee_price_filter(analysisVo.getFee_price());
					}
				}else{
					analysisVo.setFee_user_num_sum(analysisVo.getFee_user_num());
					analysisVo.setFee_minute_sum(analysisVo.getFee_minute());
					analysisVo.setFee_price_sum(analysisVo.getFee_price());
					if(isfilter == 1){
						analysisVo.setFee_user_num(0);
						analysisVo.setFee_user_num_filter(analysisVo.getFee_user_num());
						analysisVo.setFee_minute(0);
						analysisVo.setFee_minute_filter(analysisVo.getFee_minute());
						analysisVo.setFee_price(0);
						analysisVo.setFee_price_filter(analysisVo.getFee_price());
					}
					analysisVoListMap.put(province, analysisVo);
				}
			}
			
			if(analysisVoListMap != null && analysisVoListMap.size() > 0){
				analysisVoList.clear();
				for(AnalysisVo analysisVo : analysisVoListMap.values()){
					if(analysisVoMap != null && analysisVoMap.size() > 0){
						analysisVo.setUser_num(analysisVoMap.get(analysisVo.getProvince()));
					}
					analysisVoList.add(analysisVo);
				}
			}
		}
		
		ExcelCreate excelCreate = new ExcelCreate();
		excelCreate.createRow(0);
		writeAnalsisProvinceTitle(excelCreate);
		writeAnalsisProvinceBill(excelCreate,analysisVoList);
		
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			excelCreate.downloadExcel(excelCreate.getWorkbook(), response,exportName);
		} catch (IOException e) {
			logger.debug("下载业务结算异常：" + ExceptionUtils.getFullStackTrace(e));
		}
		
		return null;
	}
	
	private void writeAnalsisProvinceTitle(ExcelCreate excelCreate){
		excelCreate.setCellTitle(0, "省份");
		excelCreate.setCellTitle(1, "用户数量");
		excelCreate.setCellTitle(2, "计费数量");
		excelCreate.setCellTitle(3, "同步数量");
		excelCreate.setCellTitle(4, "过滤数量");
		excelCreate.setCellTitle(5, "计费时长(分)");
		excelCreate.setCellTitle(6, "同步时长(分)");
		excelCreate.setCellTitle(7, "过滤时长(分)");
		excelCreate.setCellTitle(8, "总收入(元)");
		excelCreate.setCellTitle(9, "同步收入(元)");
		excelCreate.setCellTitle(10, "过滤收入(元)");
	}
	
	private void writeAnalsisProvinceBill(ExcelCreate excelCreate,List<AnalysisVo> analysisVoList){
		int currentRow = 1;
		if(analysisVoList != null && analysisVoList.size() > 0){
			for(AnalysisVo analysisVo : analysisVoList){
				excelCreate.createRow(currentRow);
				
				excelCreate.setCellTitle(0, analysisVo.getProvince());
				excelCreate.setCellTitle(1, String.valueOf(analysisVo.getUser_num()));
				excelCreate.setCellTitle(2, String.valueOf(analysisVo.getFee_user_num_sum()));
				excelCreate.setCellTitle(3, String.valueOf(analysisVo.getFee_user_num()));
				excelCreate.setCellTitle(4, String.valueOf(analysisVo.getFee_user_num_filter()));
				excelCreate.setCellTitle(5, String.valueOf(analysisVo.getFee_minute_sum()));
				excelCreate.setCellTitle(6, String.valueOf(analysisVo.getFee_minute()));
				excelCreate.setCellTitle(7, String.valueOf(analysisVo.getFee_minute_filter()));
				excelCreate.setCellTitle(8, String.valueOf(analysisVo.getFee_price_sum()));
				excelCreate.setCellTitle(9, String.valueOf(analysisVo.getFee_price()));
				excelCreate.setCellTitle(10, String.valueOf(analysisVo.getFee_price_filter()));
				
				currentRow++;
			}
		}
	}
	
	/**进入详情页面*/
	public String getDetail(){
		/**获得渠道信息列表*/
		configLowerPartnerVoList = configLowerPartnerDao.getConfigLowerPartnerList();
		spnumberList = null;
		term = null;
		userBillVoList = null;
		
		pageInfo = null;
		pageIndexs = "";
		return SUCCESS;
	}
	/**查询详情信息*/
	public String getDetailAnalysis(){
		/**获得主叫号码*/
		String mobile = term.getMobile();
		/**获得渠道信息*/
		int lower_id = term.getLower_id();
		/**获得业务号码*/
		String sp_number = term.getSp_number();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		/**根据渠道ID获得渠道长号码列表*/
		spnumberList = configRouterDao.getSpnumberPartnerList(lower_id);
		
		int total = analysisDao.getDetailAnalysisTotal(mobile, sp_number, lower_id, time_begin, time_end);
		if(total == 0){
			userBillVoList = null;
			return SUCCESS;
		}
		
		pageInfo = new PageInfo();
		pageInfo.search(total);
		int currPage = 0;
		if (pageIndexs != null && !"".equals(pageIndexs)) {
			currPage = Integer.parseInt(pageIndexs);
		}
		if (currPage != 0) {
			pageInfo.turnToPage(currPage);
		}
		
		userBillVoList = analysisDao.getDetailAnalysis(mobile, sp_number, lower_id, time_begin, time_end, pageInfo);
		
		return SUCCESS;
	}
	
	public String exportAnalsisDetail(){
		/**获得主叫号码*/
		String mobile = term.getMobile();
		/**获得渠道信息*/
		int lower_id = term.getLower_id();
		/**获得业务号码*/
		String sp_number = term.getSp_number();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		/**根据渠道ID获得渠道长号码列表*/
		spnumberList = configRouterDao.getSpnumberPartnerList(lower_id);
		
		userBillVoList = analysisDao.getDetailAnalysis(mobile, sp_number, lower_id, time_begin, time_end, null);
		
		ExcelCreate excelCreate = new ExcelCreate();
		excelCreate.createRow(0);
		writeAnalsisDetailTitle(excelCreate);
		writeAnalsisDetailBill(excelCreate,userBillVoList);
		
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			excelCreate.downloadExcel(excelCreate.getWorkbook(), response,exportName);
		} catch (IOException e) {
			logger.debug("下载详细数据异常：" + ExceptionUtils.getFullStackTrace(e));
		}
		return null;
	}
	
	private void writeAnalsisDetailTitle(ExcelCreate excelCreate){
		excelCreate.setCellTitle(0, "手机号");
		excelCreate.setCellTitle(1, "业务长号码");
		excelCreate.setCellTitle(2, "开始时间");
		excelCreate.setCellTitle(3, "结束时间");
		excelCreate.setCellTitle(4, "计费时长(分)");
		excelCreate.setCellTitle(5, "计费时长(秒)");
		excelCreate.setCellTitle(6, "省份");
		excelCreate.setCellTitle(7, "城市");
		excelCreate.setCellTitle(8, "创建时间");
		excelCreate.setCellTitle(9, "是否过滤");
		excelCreate.setCellTitle(10, "同步状态");
	}
	
	private void writeAnalsisDetailBill(ExcelCreate excelCreate,List<UserBillVo> userBillVoList){
		int currentRow = 1;
		if(userBillVoList != null && userBillVoList.size() > 0){
			for(UserBillVo userBillVo : userBillVoList){
				excelCreate.createRow(currentRow);
				
				excelCreate.setCellTitle(0, userBillVo.getMobile());
				excelCreate.setCellTitle(1, userBillVo.getSp_number());
				excelCreate.setCellTitle(2, String.valueOf(userBillVo.getTime_begin()));
				excelCreate.setCellTitle(3, String.valueOf(userBillVo.getTime_end()));
				excelCreate.setCellTitle(4, String.valueOf(userBillVo.getMinute()));
				excelCreate.setCellTitle(5, String.valueOf(userBillVo.getSecond()));
				excelCreate.setCellTitle(6, String.valueOf(userBillVo.getProvince()));
				excelCreate.setCellTitle(7, String.valueOf(userBillVo.getCity()));
				excelCreate.setCellTitle(8, String.valueOf(userBillVo.getAdd_date()));
				if(userBillVo.getIsfilter() == 0){
					excelCreate.setCellTitle(9, "未过滤");
				}else {
					excelCreate.setCellTitle(9, "已过滤");
				}
				
				if(userBillVo.getDispatcher_status() == 0){
					excelCreate.setCellTitle(10, "未同步");
				}else{
					excelCreate.setCellTitle(10, String.valueOf(userBillVo.getDispatcher_status()));
				}
				
				currentRow++;
			}
		}
	}
	
	
	public IAnalysisDao getAnalysisDao() {
		return analysisDao;
	}
	public void setAnalysisDao(IAnalysisDao analysisDao) {
		this.analysisDao = analysisDao;
	}
	public IConfigLowerPartnerDao getConfigLowerPartnerDao() {
		return configLowerPartnerDao;
	}
	public void setConfigLowerPartnerDao(IConfigLowerPartnerDao configLowerPartnerDao) {
		this.configLowerPartnerDao = configLowerPartnerDao;
	}
	public IConfigRouterDao getConfigRouterDao() {
		return configRouterDao;
	}
	public void setConfigRouterDao(IConfigRouterDao configRouterDao) {
		this.configRouterDao = configRouterDao;
	}
	public List<AnalysisVo> getAnalysisVoList() {
		return analysisVoList;
	}
	public void setAnalysisVoList(List<AnalysisVo> analysisVoList) {
		this.analysisVoList = analysisVoList;
	}
	public List<ConfigLowerPartnerVo> getConfigLowerPartnerVoList() {
		return configLowerPartnerVoList;
	}
	public void setConfigLowerPartnerVoList(List<ConfigLowerPartnerVo> configLowerPartnerVoList) {
		this.configLowerPartnerVoList = configLowerPartnerVoList;
	}
	public List<String> getSpnumberList() {
		return spnumberList;
	}
	public void setSpnumberList(List<String> spnumberList) {
		this.spnumberList = spnumberList;
	}
	public List<UserBillVo> getUserBillVoList() {
		return userBillVoList;
	}
	public void setUserBillVoList(List<UserBillVo> userBillVoList) {
		this.userBillVoList = userBillVoList;
	}
	public TermVo getTerm() {
		return term;
	}
	public void setTerm(TermVo term) {
		this.term = term;
	}
	public String getExportName() {
		return exportName;
	}
	public void setExportName(String exportName) {
		this.exportName = exportName;
	}
	public PageInfo getPageInfo() {
		return pageInfo;
	}
	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}
	public String getPageIndexs() {
		return pageIndexs;
	}
	public void setPageIndexs(String pageIndexs) {
		this.pageIndexs = pageIndexs;
	}
}
